<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>Towards TinyOS for 8051</title>
<meta name="author" content="Anders Egeskov Petersen, Sidsel Jensen, Martin Leopold" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:date: $Date: 2009-02-03 23:07:32 $
:version: $Revision: 1.9 $
:copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
*/
body {
  font-family: Times;
  font-size: 16px;
}

.first {
  margin-top: 0 ! important }

.last {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dd {
  margin-bottom: 0.5em }

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.attention, div.caution, div.danger, div.error, div.hint,
div.important, div.note, div.tip, div.warning, div.admonition {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

div.hint p.admonition-title, div.important p.admonition-title,
div.note p.admonition-title, div.tip p.admonition-title,
div.admonition p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em }

div.footer, div.header {
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 0em 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1 {
  font-family: Arial, sans-serif;
  font-size: 20px;
}

h1.title {
 text-align: center;
 font-size: 32px;
}

h2 {
 font-size: 16px;
 font-family: Arial, sans-serif;
}

h2.subtitle {
  text-align: center }

h3 {
 font-size: 12px;
 font-family: Arial, sans-serif;
}

hr {
  width: 75% }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.line-block {
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee;
  border-color: #000000;
  border-width: thin; 
  font-size: 14px
}

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.option-argument {
  font-style: italic }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

table {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.citation {
  border-left: solid thin gray ;
  padding-left: 0.5ex }

table.docinfo {
  margin: 2em 4em;
}

table.footnote {
  border-left: solid thin black ;
  padding-left: 0.5ex }

td, th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

th.docinfo-name, th.field-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap;
  }

h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  font-size: 100% }

tt {}

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="towards-tinyos-for-8051">
<h1 class="title">Towards TinyOS for 8051</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr class="field"><th class="docinfo-name">TEP:</th><td class="field-body">121</td>
</tr>
<tr class="field"><th class="docinfo-name">Group:</th><td class="field-body">TinyOS 8051 Working Group</td>
</tr>
<tr class="field"><th class="docinfo-name">Type:</th><td class="field-body">Informational</td>
</tr>
<tr><th class="docinfo-name">Status:</th>
<td>Draft</td></tr>
<tr class="field"><th class="docinfo-name">TinyOS-Version:</th><td class="field-body">1.x</td>
</tr>
<tr><th class="docinfo-name">Author:</th>
<td>Anders Egeskov Petersen, Sidsel Jensen, Martin Leopold</td></tr>
<tr class="field"><th class="docinfo-name">Draft-Created:</th><td class="field-body">15-Dec-2005</td>
</tr>
<tr class="field"><th class="docinfo-name">Draft-Version:</th><td class="field-body">1</td>
</tr>
<tr class="field"><th class="docinfo-name">Draft-Modified:</th><td class="field-body">27-Mar-2006</td>
</tr>
<tr class="field"><th class="docinfo-name">Draft-Discuss:</th><td class="field-body">TinyOS 8051 Working Group List &lt;Tinyos-8051wg at mail.millennium.berkeley.edu&gt;</td>
</tr>
</tbody>
</table>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">This memo is informational. It will hopefully be a basis for
discussions and suggestions for improvements.  Distribution of this
memo is unlimited.  This memo is in full compliance with TEP 1.</p>
</div>
<div class="section">
<h1><a id="abstract" name="abstract">Abstract</a></h1>
<p>This TEP covers our effort of porting <a class="reference" href="http://www.tinyos.net">TinyOS</a> to the nRF24E1
platform. We ported the basic modules of TinyOS: Timer, UART, ADC and
LEDS.</p>
</div>
<div class="section">
<h1><a id="project-outline" name="project-outline">1. Project Outline</a></h1>
<p>The original 8 bit 8051 chip is a member of the <a class="reference" href="http://www.intel.com/design/mcs51">mcs51 family</a> and
was developed in 1980 by Intel. It is still to this date one of the most
widely used microcontrollers. Porting TinyOS to the 8051 System on chip
architecture makes perfect sense - the mcs51 family has been thoroughly
tested, it is relatively cheap and it has a reasonable small footprint -
which makes it ideal for embedded solutions and sensor networks.</p>
<p>For this work, we use a Nordic Semiconductor VLSI nRF24E1
evaluation-board [NSC]. The board contains an Intel 8051 compatible MCU
with 4KB program memory, a 16 MHz clock, 3 different Timers (one being
8052 compatible), a 2.4 GHz wireless RF transceiver and 9 input 10 bit
ADC, SPI and a RS232 Serial interface. The nRF24E1 board was chosen
because the radio component matches the radio on the specially designed
DIKU/DTU HogthrobV0 [HOG] boards used for research purposes at DIKU
[PEH].</p>
<p>We ported a subset of TinyOS for the 8051 platform consisting of the
Timer, UART, ADC and LED modules. We did not port the radio module and
the underlying SPI-bus code.</p>
<p>This works attacks the two most immediate problems when porting TinyOS
to 8051: the toolchain and the hardware abstraction components. The
first problem when porting TinyOS to 8051-based platforms concerns the
toolchain. The Gcc compiler does not support 8051. This is a major issue
as the code generated by the NesC preprocessor is tailored for gcc.  The
second problem concerns the hardware abstraction components that must be
specialized to the 8051 idiosyncracies. In a perfect world, such a
specialization should not require to modify any interface.
Unfortunately, we needed to modify some of the interfaces to accomodate
the 8051 features.</p>
<p>This work was done under the supervision of Martin Leopold at University
of Copenhagen.</p>
</div>
<div class="section">
<h1><a id="project-approach" name="project-approach">2. Project Approach</a></h1>
<p>The approach to the porting project has been pragmatic. The focus has
been on producing working code, so testing and debugging have been key
elements of our work. The process has been to implement new
functionality in small iterative steps and do testing simultaneously.</p>
<p>To bootstrap the development without a JTAG module or alike, we built a
small LED expansion board attachable to the port logic. The LEDs was an
easy way to get instant low level test output. We also built a small
stimulator based on a potentiometer (variable resistor) to get valid
input from the ADC pins.</p>
<dl class="docutils">
<dt>The following TinyOS application programs have been written and tested:</dt>
<dd><ul class="first last simple">
<li>Empty - test of port logic and tool chain</li>
<li>mcsatomic - test of atomic and interrupts</li>
<li>mcsBlink - test of LEDs</li>
<li>mcsBlinkTimer - test of Timers using LEDs</li>
<li>mcsSerialTest - test of UART code, simple input/output one char</li>
<li>mcsSerialTest2 - test of multiple byte output</li>
<li>mcsTimerSerialTest - test of UART controlled by Timer interrupts</li>
<li>mcsADC - test of ADC code with Timer and LEDs</li>
</ul>
</dd>
</dl>
</div>
<div class="section">
<h1><a id="development-environment-and-tool-chain" name="development-environment-and-tool-chain">3. Development Environment and Tool Chain</a></h1>
<p>The following subsections describe the different development tools,
their selection and interconnection.</p>
<div class="section">
<h2><a id="selection-of-development-tools-compilers" name="selection-of-development-tools-compilers">3.1 Selection of Development Tools/Compilers</a></h2>
<p>A large number of 8051 compilers exist primarily for the DOS and Windows
platforms. We have focused on two popular and regularly updated
compilers: <a class="reference" href="http://www.keil.com">KEIL</a> and the Small Device C Compiler (SDCC).</p>
<p><a class="reference" href="http://sdcc.sourceforge.net">SDCC</a> is an open source project hosted on the Sourceforge website,
whereas the KEIL C51 compiler is a commercial compiler and Integrated
Development Environment (IDE). The debugger for SDCC (SDCDB) is still
fairly experimental. The KEIL suite runs on the Windows platform, and
has a good interactive debugger and simulator. KEIL and SDCC accepts
roughly the same syntactical dialect, which eases the work of moving
between the two compilers.</p>
<p>During our work with SDCC and SDCDB we encountered numerous problems and
bugs. SDCC 2.4.0 suddenly returned 'fatal compiler errors' with no
apparent reason. After an update of SDCC from version 2.4.0 to 2.5.0
(most recent release) the error disappeared, the code compiled, but it
still did not work correctly on the board. We also discovered a serious
problem regarding sign bits in SDCC 2.5.0. SDCC made a type error when
reading a 32 bit signed value. Apparently SDCC did not interpret the
sign bit correctly, so a very small negative number was interpreted as a
very large positive number as if the value was unsigned. KEIL however
interprets the value correctly. The bug was submitted by us and fixed by
the SDCC development team in just two days, but the timer module still
does not work using SDCC.</p>
<p>Our attempts using SDCC's debugger/simulator (SDCDB) was equally
troublesome. SDCDB simply stopped at address 0, and running or stepping
through the code returned us to the UNIX prompt with no error message.
Without SDCDB, we had no debug possibility and we were forced to rethink
the tool chain. We decided to substitute SDCC with the KEIL development
kit. This gave us a working debug environment - with minimal change to
the already produced code.</p>
</div>
<div class="section">
<h2><a id="our-recommendation" name="our-recommendation">3.1.1 Our Recommendation</a></h2>
<p>In our experience the SDCC compiler and associated tools are not yet
mature enough to support our development. We recommend pursuing other
alternatives such as KEIL or other compiler suites.</p>
<p>We continue to mention SDCC in the remaining text, because we encourage
the use of open source software and cross-platform development. We hope
SDCC will prove an reliable alternative in the future.</p>
</div>
<div class="section">
<h2><a id="tool-chain-overview" name="tool-chain-overview">3.2 Tool Chain Overview</a></h2>
<p>The following figure and sections are an overview of the current tool
chain. The tool chain is based on TinyOS 1.x, NesC 1.1.3, avr-gcc 3.4.3,
PERL v. 5.8.6 and SDCC 2.5.4 or KEIL C51 version 7.20.</p>
<p>Each step in the tool chain will be explained in the section below.</p>
<pre class="literal-block">
             Mangle-
TinyOS       script
-----&gt; app.c -----&gt; app_mangled.c --------&gt; app.hex ------&gt; nRF24E1
 NesC         PERL                SDCC/KEIL         nRFPROG
</pre>
</div>
<div class="section">
<h2><a id="description-of-the-tool-chain" name="description-of-the-tool-chain">3.3 Description of the Tool Chain</a></h2>
<p>The compilation of the TinyOS test program outputs two files, a
'main.exe' file and an 'app.c' file. The 'app.c' file contains all the
needed code to run the TinyOS application. However the C code produced
by NesC cannot be compiled for the 8051 platform directly.</p>
<p>One solution could be to alter the syntax NesC produces for this
specific platform, by modifying the source for NesC. However as a first
step we chose not to make changes to NesC, but instead changed the
content of the NesC output file 'app.c'. We inserted an extra step in
the tool chain in the form of a mangle script. The mangle script works
as the rope, tying the output from NesC to the input of SDCC or KEIL.</p>
<p>After running the mangling script on the 'app.c' file we obtain an
'app_mangled.c' file which can be compiled by either SDCC or KEIL. This
produces a hex file that is transferred to the chip by the nRFPROG
software.</p>
</div>
<div class="section">
<h2><a id="description-of-the-mangling-script" name="description-of-the-mangling-script">3.4 Description of the Mangling Script</a></h2>
<p>The mangling script is written in PERL, a commonly used general purpose
scripting language with powerful pattern matching capabilities and
extensive handling of regular expressions. The mangle script handles all
currently known problems, and it can easily be expanded to handle
additional alterations.</p>
<p>To run the mangle script use the following syntax:</p>
<dl class="docutils">
<dt>&quot;./sdccMangleAppC.pl -KEIL -file build/mcs51/app.c &gt;</dt>
<dd>build/mcs51/app_mangled.c&quot;</dd>
</dl>
<p>or</p>
<dl class="docutils">
<dt>&quot;./sdccMangleAppC.pl -SDCC -file build/mcs51/app.c &gt;</dt>
<dd>build/mcs51/app_mangled.c&quot;</dd>
</dl>
<p>The 'sdccMangleAppC.pl' script handles a number of needed alterations:</p>
<blockquote>
<ul class="simple">
<li>it alters the SFR and SBIT declarations for SDCC and KEIL
respectively</li>
<li>it convert 64 bit data types to 32 bit</li>
<li>it alters the reserved SDCC keyword data to _data</li>
<li>it removes inlining directives</li>
<li>it removes preprocessor line numbering</li>
<li>it alters $ in identifiers to underscore</li>
<li>it alters GCC interrupt declaration to SDCC syntax</li>
</ul>
</blockquote>
<p>Each of these alterations will be explained in the sections below.</p>
</div>
<div class="section">
<h2><a id="sfr-and-sbit-declarations" name="sfr-and-sbit-declarations">3.4.1 SFR and SBIT Declarations</a></h2>
<p>In order to make TinyOS accept the 8051 special function registers (SFR)
and special bit variables (SBIT), we have included them into the TinyOS
8051 platform folder as a 8051.h file.</p>
<p>SFRs are located on an address dividable by 8, whereas an SBIT addresses
a specific bit within these SFR.</p>
<p>In order to make TinyOS accept the SFRs we have type defined them in the
NesC code as:</p>
<blockquote>
typedef int sfr;
sfr P0 __attribute((x80));</blockquote>
<p>which is altered to</p>
<blockquote>
//typedef int sfr;
sfr at 0x80 P0;</blockquote>
<p>for the SDCC compiler in the mangle script and</p>
<blockquote>
sfr P0 = 0x80;</blockquote>
<p>for the KEIL compiler and similar for the SBIT declarations.</p>
<p>NOTE: The SDCC website refers to a PERL script (keil2sdcc.pl - last
updated June 2003) for translating SFR and SBIT declarations from KEIL
to SDCC, but it produces code with illegal syntax, so either do not use
it, or alter it to produce code with the right syntax.</p>
</div>
<div class="section">
<h2><a id="sdcc-keil-data-types" name="sdcc-keil-data-types">3.4.2 SDCC/KEIL Data Types</a></h2>
<p>TinyOS and SDCC/KEIL do not support the same data types, so some
alterations were needed to compile the code with SDCC and KEIL.</p>
<dl class="docutils">
<dt>SDCC/KEIL supports the following data types:</dt>
<dd><ul class="first last simple">
<li>char (8 bits, 1 byte)</li>
<li>short (16 bits, 2 bytes)</li>
<li>int (16 bits, 2 bytes)</li>
<li>long (32 bit, 4 bytes)</li>
<li>float (32 bit, 4 bytes).</li>
</ul>
</dd>
</dl>
<p>TinyOS supports an extra data type - 64 bit long long (u)int64_t. Since
we are working with software that does not support this data type, on a
very small hardware memory model, we decided to change the NesC 64 bit
data types to 32 bit. This is done in the mangling script.</p>
</div>
<div class="section">
<h2><a id="reserved-keywords-in-sdcc" name="reserved-keywords-in-sdcc">3.4.3 Reserved Keywords in SDCC</a></h2>
<p>A number of keywords are reserved in SDCC. Half of them represent a
directive to the compiler, defining which memory segment on the nRF24E1
the specific lines of code refer to. To ensure that the developer does
not break code by unintentionally and unaware of their effect use them
fx. as a variable name in the NesC code, they need to be replaced or
altered to something else, e.g. data to _data, before compiling for
SDCC. Right now the mangle script only handles the reserved keyword
data. None of the other keywords except SFR, SBIT and interrupt are
currently in use in the code. This might pose as a problem to future
work, but the mangle script can easily be expanded to handle misuse of
the other keywords.</p>
<p>However, if the code size increases significantly in the future, it
might be nessecary to insert the keywords into the code, to support the
architectures segmented memory model. Right now, everything is stored in
the directly addressable memory segment, which is quite small.</p>
<dl class="docutils">
<dt>The reserved keywords are:</dt>
<dd><ul class="first last simple">
<li>data / near</li>
<li>xdata / far</li>
<li>idata / pdata</li>
<li>code</li>
<li>bit</li>
<li>SFR / SBIT</li>
<li>interrupt</li>
<li>critical</li>
</ul>
</dd>
</dl>
<p>Variables declared with keyword storage class data/near will be
allocated in the directly addressable portion of the internal RAM. This
is the default option for the small memory model. Variables declared
with storage class xdata/far will be placed in external RAM, which is
default for the large memory model.</p>
<p>Variables declared with keyword storage class idata will be allocated in
the indirectly addressable portion of the internal RAM. The first 128
byte of idata physically access the same RAM as the data memory. The
original 8051 had 128 byte idata, but nowadays most devices have 256
byte idata memory. Paged xdata access (pdata) is just as
straightforward.</p>
<p>The different memory segments that the keywords apply to, can be seen in
the figure below.</p>
<p>nRF24E1 Internal Data Memory Structure Overview:</p>
<pre class="literal-block">
                       IRAM                  SFR
                +---------------------+---------------------+
            FFh |                     |                     | FFh
                |   Accessible by     |   Accessible by     |
Upper 128 bytes |   indirect          |   direct            |
                |   addressing only   |   addressing only   |
            80h |                     |                     | 80h
                +---------------------+---------------------+
            7Fh |                     |
                | Addressable by      |
Lower 128 bytes | direct and          |
                | indirect addressing |
            00h |                     |
                +---------------------+
</pre>
<p>The prefered memory model can be defined in SDCC through CLI option
--model-small or --model-large - the small memory model is default. In
KEIL it can be changed through selecting it in the options pane for the
target.</p>
</div>
<div class="section">
<h2><a id="removal-of-inlining" name="removal-of-inlining">3.4.4 Removal of inlining</a></h2>
<p>NesC assumes that GCC is being used for the final compilation. GCC
supports inline functions and can be made to optimize code quite
aggressively, so the code generated by NesC does not need to be very
efficient. Unfortunately SDCC does not support code inlining, so the
inline statements have to be removed, when compiling for SDCC.</p>
<p>Lines with the following format are affected:</p>
<p>static inline void TOSH_sleep(void );
static __inline void TOSH_SET_RED_LED_PIN(void);
__inline void__nesc_enable_interrupt(void);</p>
<p>Lines with the noinline attribute is substituted with the
#pragma NO_INLINE.</p>
</div>
<div class="section">
<h2><a id="removal-of-preprocessor-line-numbering" name="removal-of-preprocessor-line-numbering">3.4.5 Removal of Preprocessor Line Numbering</a></h2>
<p>Also NesC produce preprocessor line number meta data, to allow the
compiler to report error messages referring to the original code. We do
not really need them for anything, so we filter them out to minimize the
code size. It also eases the code reading significantly. If needed for
debug purposes the regular expression in the mangle script which remove
them can be commented out.</p>
</div>
<div class="section">
<h2><a id="change-in-identifiers" name="change-in-identifiers">3.4.6 Change $ in Identifiers</a></h2>
<p>The SDCC compiler is very strict when it comes to valid symbols in
identifiers. NesC produce GCC-code which inserts $ as a separator
character in identifiers. We mangle the $ to two underscores in order to
enable SDCC/KEIL to compile.</p>
</div>
<div class="section">
<h2><a id="interrupt-vectors" name="interrupt-vectors">3.4.7 Interrupt Vectors</a></h2>
<p>The syntax for declaration of interrupt vectors are different in GCC and
SDCC/KEIL. So we mangle the interrupt declaration:</p>
<p>From: void __attribute((interrupt)) __vector_5(void)
To:   void __vector_5(void) interrupt 5</p>
<p>Additionally KEIL does not understand that the interrupt vector is
defined previous to its use. So we remove the forward declaration of the
vectors in the mangle script, when compiling for KEIL.</p>
</div>
</div>
<div class="section">
<h1><a id="tinyos-modifications" name="tinyos-modifications">4. TinyOS Modifications</a></h1>
<p>TinyOS is based on modules with different levels of hardware
abstraction. When porting TinyOS to a new platform, you change the
underlying hardware dependencies in TinyOS, and you have to rebuild the
modules bottom up. Hence, it has been necessary to modify a number of
modules in TinyOS. The figure below shows the topological hierarchy of
the TinyOS modules we have focused on. By far, most of the work has been
done in the Hardware Presentation Layer (HPL), but certain changes also
affected the higher abstractions, such as changes in interfaces and
interrupt handling.</p>
<p>Modified TinyOS modules overview:</p>
<pre class="literal-block">
+------------------------------------------------------------+
|                     TinyOS Application                     |  App
+------------------------------------------------------------+
  \/    /\            \/    /\           \/   /\      \/  /\   -----
+----------+        +----------+       +---------+  +--------+
|  Timer   |        |   UART   |       |   ADC   |  |  LEDs  |  HAL
+----------+        +----------+       +---------+  +--------+
  \/    /\            \/    /\           \/   /\               -----
+----------+  +---------------------+  +---------+
| HPLClock |  |       HPLUART       |  | HPLADC  |      \/      HPL
+----------+  +---------------------+  +---------+
  \/    /\        \/         \/  /\      \/   /\               -----
+----------+  +--------+   +--------+  +---------+  +--------+
|  Timer2  |  | Timer1 | &gt; | Serial |  | Sensors |  |  Port  |  HW
+----------+  +--------+   |  Port  |  +---------+  +--------+
                          +--------+
</pre>
<p>The following sections describe the changes to the four groups of modules.</p>
<div class="section">
<h2><a id="hplclock-and-related-modules" name="hplclock-and-related-modules">4.1 HPLClock and related modules</a></h2>
<p>The 8051 chip has three independent timer/counter circuits: Timer0,
Timer1 and Timer2, which can run in various modes. Each timer/counter
consists of a 16-bit register that is accessible to software as three
SFRs (TL0/TH0, TL1/TH1 and TL2/TH2). Timer0 and Timer1 can be used as 13
or 16 bit timer/counter or as 8 bit counters with auto-reload. Timer2 is
only capable of running as a 16 bit timer/counter, but can remain as
such even in auto-reload mode. Reload is desirable for loading the clock
with a start value.</p>
<p>We have chosen to use Timer2 for the clock module, since it gives our
design a maximum clock interval of 49.15 ms at a 16 MHz system clock.
Using a different timer circuit would limit the interval to 0.192 ms,
which would result in a great deal of interrupts and consume processing
power for administrational overhead.</p>
</div>
<div class="section">
<h2><a id="timer" name="timer">4.1.1 Timer</a></h2>
<p>The Timer module (HAL) uses the HPLClock module to handle the hardware
timing.  These two modules communicate through the clock interface.
However, the standard TinyOS clock interface is designed for an MCU with
a more flexible prescaler, then the 8051 chip is equipped with. The 8051
is limited to a prescaler with a factor of 1/4 or 1/12 of the CPU clock
frequency, whereas the TinyOS clock interface currently uses an 8 bit
prescaler and an 8 bit timer.  Because of the 8051s limited prescaler
options, and the possibility to use a 16 bit timer/counter to compensate
for the reduced prescaler options, we decided to widen the clock
interface from 8 to 16 bit. We are using the factor 1/4 for the
prescaler.</p>
<p>The interface change has affected the following methods:
result_t setRate(uint16_t interval, char scale)
void     setInterval(uint16_t value)
void     setNextInterval(uint16_t value)
uint16_t getInterval()
result_t setIntervalAndScale(uint16_t interval, uint8_t scale)
uint16_t readCounter()
void     setCounter(uint16_t n)</p>
<dl class="docutils">
<dt>See:</dt>
<dd>Clock.h
Clock.nc
HPLClock.nc
TimerM.nc
TimerC.nc
8051.h</dd>
</dl>
</div>
<div class="section">
<h2><a id="hpluart" name="hpluart">4.2 HPLUART</a></h2>
<p>The UART is depending on a timer to generate a baud rate for the serial
port. The architecture only allows two of the three timers (Timer1 or
Timer2), to act as such. Since Timer2 is already used by the clock
module, this leaves only Timer1 available for the UART module.</p>
<p>When using Timer1 as the baud rate generator, 5 different baud rates can
be obtained: 1.20 KiB/s, 2.4 KiB/s, 4.8 KiB/s, 9.6 KiB/s or 19.2 KiB/s.
We chose to use a baud rate of 19.2 KiB/s with 8 data bits, no parity
and one stop bit, since this speed is commonly used and the fastest
speed possible using this timer.</p>
<p>We have also expanded the HPLUART interface to include a put2 method.
This method is able to send more than one byte, by taking two pointers
as arguments. These pointers refer to the first and last bytes to be
sent. The HPLUART interrupt handler was also modified to take the
multiple byte data into account.</p>
<dl class="docutils">
<dt>See:</dt>
<dd>8051.h
HPLUART.nc
HPLUARTC.nc
HPLUARTM.nc</dd>
</dl>
</div>
<div class="section">
<h2><a id="hpladc" name="hpladc">4.3 HPLADC</a></h2>
<p>The TinyOS standard ADC interface was developed for the AVR which
includes hardware functionality for repetitive sampling at a given
interval.  Implementing this functionality on the 8051, which does not
support this in hardware, would require use of the last timer. We chose
not to implement repetitive sampling, therefore the setSampleRate method
currently has no use.</p>
<dl class="docutils">
<dt>See:</dt>
<dd>8051.h
ADCM.nc
HPLADCC.nc
HPLADCM.nc</dd>
</dl>
</div>
<div class="section">
<h2><a id="leds" name="leds">4.4 LEDS</a></h2>
<p>TinyOS features three standard LEDs (Red, Green and Yellow), but the
nRF24E1 evaluation board is not equipped with programmable LEDs so we
used the general purpose ports (GPIO).</p>
<p>The standard 8051 platform features four 8 bit GPIO, however the nRF24E1
evaluation board is only equipped with two ports: Port0 and Port1, where
Port0 has eight bits and Port1 has only three bits.</p>
<p>Intuitively the best solution would have been to place the standard
three TinyOS LED bits on Port1, but unfortunately we were unable to
control the most significant bit on Port1, since it is hard-wired as
input and controlled by external SPI_CTRL. The Yellow LED was moved to
Port0.</p>
<p>To visualize the status of the GPIO, including the three standard LEDs,
we built a LED expansion board.</p>
<dl class="docutils">
<dt>The three LEDs are currently wired to:</dt>
<dd>Red -&gt; P1.0
Green -&gt; P1.1
Yellow -&gt; P0.7.</dd>
<dt>See:</dt>
<dd>8051.h
hardware.h
mcs51hardware.h
LedsC.nc</dd>
</dl>
</div>
<div class="section">
<h2><a id="interrupts" name="interrupts">4.5 Interrupts</a></h2>
<p>In TinyOS interrupts are not implemented as a single module, they are
mainly facilitated in atomic blocks and in the init, start and stop
methods of the various HPL modules. The init, start and stop methods
only handle interrupts that are specific to the module, i.e. timer
interrupt for the HPLClock module and serial interrupt for the HPLUART
module. While the atomic block handle the enabling of global interrupts.
This is used to avoid preempting code execution in critical blocks.</p>
</div>
</div>
<div class="section">
<h1><a id="conclusion" name="conclusion">5. Conclusion</a></h1>
<p>The project have reached a plateau of development in porting TinyOS to
the 8051 platform, on which future development can be based. The basic
modules (Timer, UART, ADC and LEDS) have been implemented making 8051
accessible for the TinyOS community. However a essential module for the
field of sensor networks, the radio module, is still missing.</p>
<p>The result of our work will be uploaded to the TinyOS 8051 Working Group
website.</p>
</div>
<div class="section">
<h1><a id="future-work" name="future-work">6. Future Work</a></h1>
<p>The work presented in this TEP is short of being a complete porting of
TinyOS to the 8051 platform. Two obvious future tasks are implementing a
Radio module involving the SPI interface and Power Management for duty
cycling. The radio module is currently under development, in which the
main hurdle is the three wire SPI interface.</p>
<p>This work is done for TinyOS 1.x, but looking forward, the 8051 port
should target TinyOS 2.0. This might be a challenge with the timer
interface being so different from TinyOS 1.x.</p>
</div>
<div class="section">
<h1><a id="authors" name="authors">7. Authors</a></h1>
<div class="line-block">
<div class="line">Anders Egeskov Petersen</div>
<div class="line">University of Copenhagen, Dept. of Computer Science</div>
<div class="line">Universitetsparken 1</div>
<div class="line">DK-2100 K¯benhavn ÿ</div>
<div class="line">Denmark</div>
<div class="line"><br /></div>
<div class="line">Sidsel Jensen</div>
<div class="line">University of Copenhagen, Dept. of Computer Science</div>
<div class="line">Universitetsparken 1</div>
<div class="line">DK-2100 K¯benhavn ÿ</div>
<div class="line">Denmark</div>
<div class="line"><br /></div>
<div class="line">email - <a class="reference" href="mailto:purps&#64;diku.dk">purps&#64;diku.dk</a></div>
<div class="line"><br /></div>
<div class="line">Martin Leoold</div>
<div class="line">University of Copenhagen, Dept. of Computer Science</div>
<div class="line">Universitetsparken 1</div>
<div class="line">DK-2100 K¯benhavn ÿ</div>
<div class="line">Denmark</div>
<div class="line"><br /></div>
<div class="line">Phone +45 3532 1464</div>
<div class="line"><br /></div>
<div class="line">email - <a class="reference" href="mailto:leopold&#64;diku.dk">leopold&#64;diku.dk</a></div>
</div>
</div>
<div class="section">
<h1><a id="citations" name="citations">8. Citations</a></h1>
<table class="docutils citation" frame="void" id="nsc" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a name="nsc">[NSC]</a></td><td>Nordic Semiconductor. nRF24E1 Evalutaion board.
<a class="reference" href="http://www.nordicsemi.no/files/Product/development_tools/nRF24E1_EVBOARD_rev1_0.pdf">http://www.nordicsemi.no/files/Product/development_tools/nRF24E1_EVBOARD_rev1_0.pdf</a></td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="peh" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a name="peh">[PEH]</a></td><td>Martin Leopold. &quot;Power Estimation using the Hogthrob Prototype Platform&quot; <em>M.Sc.
Thesis, DIKU, Copenhagen University, Denmark, December 2004</em> .</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="hog" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a name="hog">[HOG]</a></td><td>Kashif Virk, Jan Madsen, Andreas Vad Lorentzen, Martin Leopold, Philippe Bonnet.
&quot;Design of A Development Platform for HW/SW Codesign ofWireless Integrated Sensor
Nodes&quot; <em>Eighth Euromicro Symposium on Digital Systems Design</em>, 2005.</td></tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
